package de.luh.slimpp.generator

import de.luh.slimpp.slimPP.ClassDefinition
import de.luh.slimpp.slimPP.Model
import de.luh.slimpp.slimPP.NamespaceDefinition
import de.luh.slimpp.slimPP.TopLevelDefinition
import java.util.ArrayList
import java.util.List

class EnhancedModel {
	val enhancedClasses = new ArrayList<EnhancedClass>
	Model model

	new(Model model) {
		this.model = model
		model.definitions.assignFullNames(emptyList)
	}

	/**
	 * Assigns all classes within a namespace its name, e.g. if class A is in namespace b
	 * then the qualified name of A will be b.A after this method was called.
	 * Applying this method makes handling NamespaceDefinitions unnecessary as they are
	 * implicitly contained in the class' qualified names then.
	 */
	private def void assignFullNames(List<TopLevelDefinition> defs, List<String> namespaces) {

		// each class directly defined in the current namespace is prepended with its name
		defs.filter(typeof(ClassDefinition)).forEach[qualifiedName.namespaces.addAll(0, namespaces)]
		
		// for all namespaces directly defined in the current namespace, we call this method
		// recursively with the namespaces appended
		for (ns : defs.filter(typeof(NamespaceDefinition))) {
			val subNamespaces = newArrayList(ns.name.name)
			subNamespaces.addAll(0, ns.name.namespaces)
			subNamespaces.addAll(0, namespaces)
			ns.definitions.assignFullNames(subNamespaces)
		}
	}
}
